<template>
    <div class="home">
        <div style="height:20px"></div>
        <el-form ref="form" :model="form" label-width="80px">
            <el-form-item label="博客标题">
                <el-input :maxlength="30" v-model="form.title"></el-input>
            </el-form-item>
            <div class="editor">
                <quill-editor class="editor" ref="myTextEditor" v-model="content" :options="editorOption"
                    @blur="onEditorBlur($event)" @focus="onEditorFocus($event)" @ready="onEditorReady($event)"
                    @change="onEditorChange($event)">
                </quill-editor>
            </div>

            <el-form-item style="margin-top:80px">
                <el-button type="primary" @click="onSubmit">确认</el-button>
                <el-button>取消</el-button>
            </el-form-item>
        </el-form>

    </div>
</template>
  <script>
export default {
    data() {
        return {
            content: null,
            editorOption: {
                modules: {
                    toolbar: [
                        ["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线
                        ["blockquote", "code-block"], // 引用  代码块
                        [{ header: 1 }, { header: 2 }], // 1、2 级标题
                        [{ list: "ordered" }, { list: "bullet" }], // 有序、无序列表
                        [{ script: "sub" }, { script: "super" }], // 上标/下标
                        [{ indent: "-1" }, { indent: "+1" }], // 缩进
                        // [{'direction': 'rtl'}],                         // 文本方向
                        [{ size: ["small", false, "large", "huge"] }], // 字体大小
                        [{ header: [1, 2, 3, 4, 5, 6, false] }], // 标题
                        [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色
                        [{ font: [] }], // 字体种类
                        [{ align: [] }], // 对齐方式
                        ["clean"], // 清除文本格式
                        ["link", "image", "video"] // 链接、图片、视频
                    ], //工具菜单栏配置
                },
                placeholder: '请在这里添加产品描述', //提示
                readyOnly: false, //是否只读
                theme: 'snow', //主题 snow/bubble
                syntax: true, //语法检测
            },
            form: {
                title: '',
                tag: [],
            },

        }
    }, // z-index
    methods: {
        // 失去焦点
        onEditorBlur(editor) { },
        // 获得焦点
        onEditorFocus(editor) { },
        // 开始
        onEditorReady(editor) { },
        // 值发生变化
        onEditorChange(editor) {
            this.content = editor.html;
            // console.log(editor);
        },
        //提交博客信息
        onSubmit() {

            console.log('submit!');
            this.addBlog();
        },
        addBlog() {
            console.log(this.form.tag);

            if (this.form.title != null && this.content != null) {
                this.$api.insertBlogOne(
                    {
                        "title": this.form.title,
                        "content": this.content
                    }
                ).then(res => {
                    console.log(res.code)
                    if (res.code === 200) {
                        this.$message({
                            message: '添加成功',
                            type: 'success',
                            duration: 300
                        });
                    } else if (res.code === 300) {
                        this.$message({
                            message: '标题重复',
                            type: 'error',
                            duration: 300
                        });
                    } else {
                        this.$message({
                            message: '未知错误',
                            type: 'error',
                            duration: 300
                        });
                    }


                });

            } else {
                this.$message({
                    message: '请补全内容，后重试',
                    type: 'error',
                    duration: 300
                });
            }
        },

    },


    computed: {
        editor() {
            return this.$refs.myTextEditor.quillEditor;
        }
    },
    mounted() {

    }, created() {



    }
}
</script>
  <style scoped lang="scss">
  .home {
      width: 95%;
      margin: auto;
  
      .editor {
          height: 500px;
          margin-bottom: 30px;
      }
  
  }
  </style>